<?php

namespace app\index\controller;


use app\common\controller\Frontend;

class Bbs extends Frontend
{

    protected $attention = [];
    public function _initialize()
    {
        parent::_initialize();
        $attentionModel = new \app\admin\model\user\attention\Bbsmodel;
        $res = $attentionModel->
                    with('bbsmodels')->
                    where('user_id', $this->userID)->
                    order('id desc')->
                    select();
        foreach ($res as $key=>$value)
        {
            $this->attention[$value['bbsmodels']['id']] =  $value['bbsmodels']['title'];
        }

        $model      = new \app\admin\model\bbs\Contents;
        $hotList    = $model->order('hits DESC,tag DESC,id DESC')->limit(5)->select();
        $this->assign('hotlist', $hotList);
        $this->assign('attention', $this->attention);
    }

    public function index()
    {
        $model = new \app\admin\model\bbs\Models;
        $list  = $model->order('weigh desc,id desc')->select();
        foreach ($list as $key=>$value)
        {
            if (array_key_exists($value['id'], $this->attention)) {
                $value['attention'] = 1;
            } else {
                $value['attention'] = 0;
            }
        }
        $this->view->assign('list', $list);
        return $this->view->fetch();
    }

    public function contents($ids = null)
    {
        $model      = new \app\admin\model\bbs\Contents;
        $list       = $model->with('users')->
                            where('model_id', $ids)->
                            order('tag DESC,weigh DESC, id DESC')->paginate(5);
        $model      = new \app\admin\model\bbs\Reply;
        foreach ($list as $key=>&$value)
        {
            $value['reply'] = $model->with('users')->where('content_id', $value['id'])->order('id desc')->find();
            $value['reply_sum'] = $model->with('users')->where('content_id', $value['id'])->order('id desc')->count();;
        }
        $model      = new \app\admin\model\bbs\Models;

        $current    = $model->where('id', $ids)->value('title');

        $this->view->assign('list', $list);
        $this->view->assign('pages', $list->render());
        $this->view->assign('current', $current);
        return $this->view->fetch();
    }

    public function addContent()
    {
        if ($this->request->isPost()) {
            $data['user_id'] = $this->userID;
            $data['model_id'] = $this->request->post('model_id');
            $data['title']   = $this->request->post('title');
            $data['content']   = $this->request->post('content');
            $model = new \app\admin\model\bbs\Contents;
            if ($model->save($data) !== false) {
                $this->success('发布成功', 'javascript:history.back(-1);');
            } else {
                $this->error($model->getError());
            }
        } else {
            $model = new \app\admin\model\bbs\Models;
            $list  = $model->order('weigh desc,id desc')->select();
            $this->view->assign('list', $list);
        }
        return $this->view->fetch();
    }

    public function editContent($ids = null)
    {
        $model = new \app\admin\model\bbs\Contents;
        $row = $model->get($ids);
        if ($this->request->isPost()) {
            $data['user_id'] = $this->userID;
            $data['model_id'] = $this->request->post('model_id');
            $data['title']   = $this->request->post('title');
            $data['content']   = $this->request->post('content');
            if ($row->save($data) !== false) {
                $row->setInc('editnum');
                $this->success();
            } else {
                $this->error($row->getError());
            }
        }
        $model = new \app\admin\model\bbs\Models;
        $list  = $model->order('weigh desc,id desc')->select();
        $this->view->assign('list', $list);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function detail($ids = null)
    {
        $model      = new \app\admin\model\bbs\Contents;
        $row        = $model->where('id', $ids)->find();

        $row->setInc('hits');

        $model      = new \app\admin\model\bbs\Reply;
        $reply      = $model->with('users')->where('content_id', $ids)->order('id DESC')->paginate(5);
        $row['reply_sum'] = $model->where('content_id', $ids)->order('id DESC')->count();
        $model      = new \app\admin\model\bbs\Replyzan;
        $modelReport      = new \app\admin\model\bbs\Report;
        foreach ($reply as $key=>$value)
        {
            $replyS = $model->where('bbsreply_id', $value['id'])->order('id desc')->select();
            $reply[$key]['is_replyzan']  = 0;
            $reply[$key]['is_report'] = 0;
            foreach ($replyS as $v) {
                if ($v['user_id'] == $this->userID)
                    $reply[$key]['is_replyzan'] = 1;
            }
            $reply[$key]['replyzan_sum'] = count($replyS);


            if ($modelReport->where(['reply_id'=>$value['id'], 'user_id'=>$this->userID])->find())
                $reply[$key]['is_report'] = 1;
        }
        $row['reply'] = $reply;

        $current = $row['title'];

        $this->view->assign('row', $row);
        $this->view->assign('pages', $row['reply']->render());
        $this->view->assign('current', $current);
        return $this->view->fetch();
    }

    public function addReply()
    {
        if ($this->request->isAjax()) {
            $data['user_id']    = $this->userID;
            $data['content_id'] = $this->request->post('content_id');
            $data['content']    = $this->request->post('content');
            $model = new \app\admin\model\bbs\Reply;
            $model->save($data);
        }
    }

    public function replyZan($ids = null, $type = null)
    {
        if ($this->request->isAjax()) {
            $model = new \app\admin\model\bbs\Replyzan;
            if ($type == 1) {
                $data['user_id'] = $this->userID;
                $data['bbsreply_id'] = $ids;
                $model->save($data);
            } else {
                $model->where(['user_id' => $this->userID, 'bbsreply_id' => $ids])->delete();
            }
        }
    }

    public function addReport($ids = null)
    {
        if ($this->request->isAjax()) {
            $data['user_id'] = $this->userID;
            $data['reply_id'] = $ids;
            $model = new \app\admin\model\bbs\Report;
            $model->save($data);
        }
    }
}
